<template>
  <el-form ref="deptForm" label-width="120px" :model="form" :rules="rules">
    <el-form-item label="部门名称" prop="name">
      <el-input v-model="form.name" style="width:80%" placeholder="1-50个字符" />
    </el-form-item>
    <el-form-item label="部门编码" prop="code">
      <el-input v-model="form.code" style="width:80%" placeholder="1-50个字符" />
    </el-form-item>
    <el-form-item label="部门负责人" prop="manager">
      <el-select v-model="form.manager" style="width:80%" placeholder="请选择">
        <el-option
          v-for="item in list"
          :key="item.id"
          :value="item.username"
          :label="item.username"
        />
      </el-select>
    </el-form-item>
    <el-form-item label="部门介绍" prop="introduce">
      <el-input v-model="form.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" size="small" @click="hSubmit">确定</el-button>
      <el-button size="small" @click="hCancel">取消</el-button>
    </el-form-item>
  </el-form>
</template>
<script>
import { getEmployessSimple } from '@/api/employess'
import { addDepartment, getDepartmentDatail, updateDepartment } from '@/api/departments'

export default {
  props: {
    id: {
      type: String,
      required: true
    },
    isEdit: {
      type: Boolean,
      required: true
    },
    originList: {
      type: Array,
      required: true
    }
  },
  data() {
    // 判断用户编号是否重复
    const validCode = (rule, value, callback) => {
      let exsitCodeList = this.originList.map(item => item.code)
      console.log('exsitCodeList', exsitCodeList)
      // 将在编辑部门的情况下筛除code
      if (this.isEdit) {
        exsitCodeList = this.originList.filter(item => item.id).map(item => item.code)
      }
      if (exsitCodeList.includes(value)) {
        callback(new Error(`改部门:${value}已经存在，请重新输入吧`))
      } else {
        callback()
      }
    }
    // 判断用户名称编码是否重复
    const validName = (rule, value, callback) => {
      // 先将判断二级标签的pid是否等一id
      console.log('判断code和id是否相等', this.originList.filter(item => item.pid === item.id))
      let exsitNameList = this.originList.filter(item => item.pid === this.id).map(item => item.name)
      if (this.isEdit) {
        const pid = this.originList.find(item => item.id === this.id).pid
        // 获取被占用的名称列表
        exsitNameList = this.originList.filter(item => item.pid === pid).filter(item => item.id !== this.id).map(item => item.name)
      }
      if (exsitNameList.includes(value)) {
        callback(new Error(`改部门名称:${value}已经存在，请重新输入吧`))
      } else {
        callback()
      }
    }
    return {
      form: {
        name: '',
        code: '',
        introduce: '',
        manager: ''

      },
      rules: {
        name: [
          { required: true, message: '请输入部门名称', trigger: 'blur' },
          { min: 2, max: 10, message: '长度在1到50个字符', trigger: 'bulr' },
          { validator: validName, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入部门编码', trigger: 'blur' },
          { min: 2, max: 10, message: '长度在1到50个字符', trigger: 'bulr' },
          { validator: validCode, trigger: 'blur' }
        ],
        introduce: [
          { required: true, message: '请输入部门管理者', trigger: 'blur' }

        ],
        manager: [
          { required: true, message: '请输入部门介绍', trigger: 'blur' },
          { min: 1, max: 300, message: '长度在1到300个字符', trigger: 'bulr' }
        ]

      },
      list: []
    }
  },
  watch: {
    id: function(new_value, old_value) {
      console.log('new_value', new_value)
      console.log('old_value', old_value)
      this.loadDepartmentDatail()
    }
  },
  created() {
    this.loadEmployeeSimple()
    if (this.isEdit) {
      this.loadDepartmentDatail()
    }
  },
  methods: {
    // 重置表单
    resetFrom() {
      this.$refs.deptForm.resetFields()
    },

    // 获取部门详情数据
    async loadDepartmentDatail() {
      const res = await getDepartmentDatail(this.id)
      console.log('111', res)
      if (res.code === 10000) {
        // 将获取到的部门详情填充到表单
        this.form = res.data
      }
    },
    // 添加子部门
    async doAdd() {
      console.log(this.from, this.id)
      try {
        const res = await addDepartment({ pid: this.id, ...this.form })
        if (res.code !== 10000) return this.$message.error(res.message)
        this.$message.success(res.message)
        // 从新渲染页面
        // 关闭弹窗
        this.$emit('success')
        console.log('2222', res)
      } catch (error) {
        console.log(error)
      }
    },
    // 实现部门编辑
    async doEdit() {
      // 发送xios
      try {
        const res = await updateDepartment(this.form)
        if (res.code !== 10000) return this.$message.error(res.message)
        this.$message.success('获取列表成功')
        // 跟新渲染页面
        this.$emit('success')
      } catch (error) {
        console.log(error)
      }
    },
    hSubmit() {
      this.$refs.deptForm.validate((valid) => {
        if (!valid) return
        this.isEdit ? this.doEdit() : this.doAdd()
      })
    },
    hCancel() {
      this.$emit('close')
    },
    async loadEmployeeSimple() {
      try {
        const res = await getEmployessSimple()
        console.log('获取个人信息', res)
        this.list = res.data
      } catch (error) {
        console.log(error)
      }
    }

  }

}
</script>
<style>

</style>
